package com.black.cat.jvm.rtda.heap.methodarea;

import com.black.cat.jvm.classfile.MemberInfo;
import com.black.cat.jvm.classfile.attribute.CodeAttribute;

/**
 * @Author blackcat
 * @create 2021/8/14 10:46
 * @version: 1.0
 * @description:方法类
 */
public class JMethod extends ClassMember{

    //操作数栈大小
    public int maxStack;
    //局部变量大小
    public int maxLocals;
    //方法字节码
    public byte[] code;

    public JMethod[] newMethods(JClass clazz, MemberInfo[] cfMethods) {
        JMethod[] JMethods = new JMethod[cfMethods.length];
        for (int i = 0; i < cfMethods.length; i++) {
            JMethods[i] = new JMethod();
            JMethods[i].clazz = clazz;
            JMethods[i].copyMemberInfo(cfMethods[i]);
            JMethods[i].copyAttributes(cfMethods[i]);
        }
        return JMethods;
    }

    public void copyAttributes(MemberInfo cfMethod) {
        CodeAttribute codeAttr = cfMethod.codeAttribute();
        if (null != codeAttr) {
            this.maxStack = codeAttr.getMaxStack();
            this.maxLocals = codeAttr.getMaxLocals();
            this.code = codeAttr.getCode();
        }
    }
}
